package com.qingxin.sys.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.SysGroup;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.SysConfigService;
import com.qingxin.sys.service.SysGroupService;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;

/**
 * 分组管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/group")
public class GroupAction extends BaseAction {
	
	@Autowired
	private SysGroupService sysGroupService;
	@Autowired
	private SysConfigService sysConfigService;
	
	@RequestMapping(value="/groupJstree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object groupJstree(String pid, String root, Integer maxGroupType, HttpServletRequest request){
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){//加载根节点
			LoginUser loginUser = getLoginUser(request);
			
			if(SystemCfg.isSystemAdminUser(loginUser)){
				if("true".equals(root)){
					List<JsTreeNode> list = new ArrayList<>();
					JsTreeNode jsTreeNode = new JsTreeNode("0", "分组", "GROUP", "groupid", true);
					jsTreeNode.setCustdata("1");
					list.add(jsTreeNode);
					return list;
				}else {
					List<TreeNode> glist = sysGroupService.getGroupTree("0", "GROUP", maxGroupType, loginUser);
					List<JsTreeNode> list = new ArrayList<>();
					for(TreeNode treeNode : glist){
						JsTreeNode jsTreeNode = new JsTreeNode(treeNode.getId(), treeNode.getName(), "GROUP", "groupid", true);
						jsTreeNode.setCustdata(treeNode.getCustdata());
						list.add(jsTreeNode);
					}
					return list;
				}
			}else if(SystemCfg.isAdminUser(loginUser)){
				String root_group = sysConfigService.getConfigValue("root_group");
				if(root_group == null){
					List<JsTreeNode> list = new ArrayList<JsTreeNode>();
					return list;
				}
				SysGroup group = sysGroupService.get(root_group);
				List<JsTreeNode> list = new ArrayList<>();
				JsTreeNode jsTreeNode = new JsTreeNode(group.getGroupid(), group.getGroupname(), "GROUP", "groupid", true);
				jsTreeNode.setCustdata(""+group.getGrouptype());
				list.add(jsTreeNode);
				return list;
			}else {
				String groupid = loginUser.getGroupid();
				if(StringUtils.isNotEmpty(groupid)){
					SysGroup group = sysGroupService.get(groupid);
					List<JsTreeNode> list = new ArrayList<>();
					JsTreeNode jsTreeNode = new JsTreeNode(group.getGroupid(), group.getGroupname(), "GROUP", "groupid", true);
					jsTreeNode.setCustdata(""+group.getGrouptype());
					list.add(jsTreeNode);
					return list;
				}else {
					List<JsTreeNode> list = new ArrayList<JsTreeNode>();
					return list;
				}
			}
		}else{
			List<TreeNode> glist = sysGroupService.getGroupTree(pid, "GROUP", maxGroupType, getLoginUser(request));
			List<JsTreeNode> list = new ArrayList<>();
			for(TreeNode treeNode : glist){
				JsTreeNode jsTreeNode = new JsTreeNode(treeNode.getId(), treeNode.getName(), "GROUP", "groupid", true);
				jsTreeNode.setCustdata(treeNode.getCustdata());
				list.add(jsTreeNode);
			}
			return list;
		}
	}
	
	/**
	 * 农资
	 * @param pid
	 * @param root
	 * @param maxGroupType
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/groupJstreeNz.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object groupJstreeNz(String pid, String root, Integer maxGroupType, HttpServletRequest request){
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){//加载根节点
			LoginUser loginUser = getLoginUser(request);
			
			if(SystemCfg.isSystemAdminUser(loginUser)){
				if("true".equals(root)){
					List<JsTreeNode> list = new ArrayList<>();
					JsTreeNode jsTreeNode = new JsTreeNode("0", "分组", "GROUP", "groupid", true);
					jsTreeNode.setCustdata("1");
					list.add(jsTreeNode);
					return list;
				}else {
					List<TreeNode> glist = sysGroupService.getGroupTreeNz("0", "GROUP", loginUser);
					List<JsTreeNode> list = new ArrayList<>();
					for(TreeNode treeNode : glist){
						JsTreeNode jsTreeNode = new JsTreeNode(treeNode.getId(), treeNode.getName(), "GROUP", "groupid", true);
						jsTreeNode.setCustdata(treeNode.getCustdata());
						list.add(jsTreeNode);
					}
					return list;
				}
			}else if(SystemCfg.isAdminUser(loginUser)){
				String root_group = sysConfigService.getConfigValue("root_group");
				if(root_group == null){
					List<JsTreeNode> list = new ArrayList<JsTreeNode>();
					return list;
				}
				SysGroup group = sysGroupService.get(root_group);
				List<JsTreeNode> list = new ArrayList<>();
				JsTreeNode jsTreeNode = new JsTreeNode(group.getGroupid(), group.getGroupname(), "GROUP", "groupid", true);
				jsTreeNode.setCustdata(""+group.getGrouptype());
				list.add(jsTreeNode);
				return list;
				
			}else {
				String groupid = loginUser.getGroupid();
				if(StringUtils.isNotEmpty(groupid) && ("0".equals(loginUser.getUser_type()) || "4".equals(loginUser.getUser_type()))){
					SysGroup group = sysGroupService.get(groupid);
					List<JsTreeNode> list = new ArrayList<>();
					JsTreeNode jsTreeNode = new JsTreeNode(group.getGroupid(), group.getGroupname(), "GROUP", "groupid", true);
					jsTreeNode.setCustdata(""+group.getGrouptype());
					list.add(jsTreeNode);
					return list;
				}else {
					List<JsTreeNode> list = new ArrayList<JsTreeNode>();
					return list;
				}
			}
		}else{
			List<TreeNode> glist = sysGroupService.getGroupTreeNz(pid, "GROUP", getLoginUser(request));
			List<JsTreeNode> list = new ArrayList<>();
			for(TreeNode treeNode : glist){
				JsTreeNode jsTreeNode = new JsTreeNode(treeNode.getId(), treeNode.getName(), "GROUP", "groupid", true);
				jsTreeNode.setCustdata(treeNode.getCustdata());
				list.add(jsTreeNode);
			}
			return list;
		}
	}
	
	/**
	 * 检测
	 * @param pid
	 * @param root
	 * @param maxGroupType
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/groupJstreeJc.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object groupJstreeJc(String pid, String root, Integer maxGroupType, HttpServletRequest request){
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){//加载根节点
			LoginUser loginUser = getLoginUser(request);
			
			if(SystemCfg.isSystemAdminUser(loginUser)){
				if("true".equals(root)){
					List<JsTreeNode> list = new ArrayList<>();
					JsTreeNode jsTreeNode = new JsTreeNode("0", "分组", "GROUP", "groupid", true);
					jsTreeNode.setCustdata("1");
					list.add(jsTreeNode);
					return list;
				}else {
					List<TreeNode> glist = sysGroupService.getGroupTreeJc("0", "GROUP", loginUser);
					List<JsTreeNode> list = new ArrayList<>();
					for(TreeNode treeNode : glist){
						JsTreeNode jsTreeNode = new JsTreeNode(treeNode.getId(), treeNode.getName(), "GROUP", "groupid", true);
						jsTreeNode.setCustdata(treeNode.getCustdata());
						list.add(jsTreeNode);
					}
					return list;
				}
			}else if(SystemCfg.isAdminUser(loginUser)){
				String root_group = sysConfigService.getConfigValue("root_group");
				if(root_group == null){
					List<JsTreeNode> list = new ArrayList<JsTreeNode>();
					return list;
				}
				SysGroup group = sysGroupService.get(root_group);
				List<JsTreeNode> list = new ArrayList<>();
				JsTreeNode jsTreeNode = new JsTreeNode(group.getGroupid(), group.getGroupname(), "GROUP", "groupid", true);
				jsTreeNode.setCustdata(""+group.getGrouptype());
				list.add(jsTreeNode);
				return list;
			}else {
				String groupid = loginUser.getGroupid();
				if(StringUtils.isNotEmpty(groupid) && ("0".equals(loginUser.getUser_type()) || "5".equals(loginUser.getUser_type()))){
					SysGroup group = sysGroupService.get(groupid);
					List<JsTreeNode> list = new ArrayList<>();
					JsTreeNode jsTreeNode = new JsTreeNode(group.getGroupid(), group.getGroupname(), "GROUP", "groupid", true);
					jsTreeNode.setCustdata(""+group.getGrouptype());
					list.add(jsTreeNode);
					return list;
				}else {
					List<JsTreeNode> list = new ArrayList<JsTreeNode>();
					return list;
				}
			}
		}else{
			List<TreeNode> glist = sysGroupService.getGroupTreeJc(pid, "GROUP", getLoginUser(request));
			List<JsTreeNode> list = new ArrayList<>();
			for(TreeNode treeNode : glist){
				JsTreeNode jsTreeNode = new JsTreeNode(treeNode.getId(), treeNode.getName(), "GROUP", "groupid", true);
				jsTreeNode.setCustdata(treeNode.getCustdata());
				list.add(jsTreeNode);
			}
			return list;
		}
	}
	
	
	/**
	 * 获取分组树
	 * @param pid
	 * @param root 是否显示根节点
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/grouptree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object grouptree(String pid, String root, Integer maxGroupType, HttpServletRequest request){
		if(StringUtils.isEmpty(pid)){//加载根节点
			LoginUser loginUser = getLoginUser(request);
			
			if(SystemCfg.isSystemAdminUser(loginUser)){
				if("true".equals(root)){
					List<TreeNode> list = new ArrayList<>();
					list.add(new TreeNode("0", "分组", "GROUP", "groupid", true));
					return list;
				}else {
					return sysGroupService.getGroupTree("0", "GROUP", maxGroupType, loginUser);
				}
			}else if(SystemCfg.isAdminUser(loginUser)){
				String root_group = sysConfigService.getConfigValue("root_group");
				if(root_group == null){
					List<JsTreeNode> list = new ArrayList<JsTreeNode>();
					return list;
				}
				SysGroup group = sysGroupService.get(root_group);
				List<JsTreeNode> list = new ArrayList<>();
				JsTreeNode jsTreeNode = new JsTreeNode(group.getGroupid(), group.getGroupname(), "GROUP", "groupid", true);
				jsTreeNode.setCustdata(""+group.getGrouptype());
				list.add(jsTreeNode);
				return list;
				
			}else {
				String groupid = loginUser.getGroupid();
				if(StringUtils.isNotEmpty(groupid)){
					SysGroup group = sysGroupService.get(groupid);
					List<TreeNode> list = new ArrayList<TreeNode>();
					list.add(new TreeNode(group.getGroupid(), group.getGroupname(), "GROUP", "groupid", true));
					return list;
				}else {
					List<TreeNode> list = new ArrayList<TreeNode>();
					return list;
				}
			}
		}else{
			return sysGroupService.getGroupTree(pid, "GROUP", maxGroupType, getLoginUser(request));
		}
	}
	
	
	/**
	 * 分组列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "sys/group/group_list";
	}
	
	
	/**
	 * 分组列表
	 * @param pid
	 * @param search
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(@RequestParam(value="leftdata")String pid, String search, PageParam pageParam, HttpServletRequest request){
		return sysGroupService.list(pid, search, getLoginUser(request), pageParam, request);
	}
	
	
	/**
	 * 添加分组信息
	 * @param modelMap
	 * @param datatype
	 * @param groupid
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, @RequestParam(value="leftdata")String pid){
		model.put("pid", pid);
		
		if(StringUtils.isNotEmpty(pid)){
			SysGroup pgroup = sysGroupService.get(pid);
			model.put("pgroup", pgroup);
		}else {
			model.put("pgroup", null);
		}
		return "sys/group/group_add";
	}

	
	/**
	 * 添加分组信息
	 * 
	 * @param group
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid SysGroup group, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		if(!group.getPid().equals("0")){
			SysGroup pGroup = sysGroupService.get(group.getPid());
			if(group.getGrouptype() == 1){
				if(pGroup.getGrouptype() > 1){
					return new SysError("该位置不可创建地区型分组");
				}
			}else if(group.getGrouptype() == 2){
				if(pGroup.getGrouptype() > 1){
					return new SysError("该位置不可创建企业型分组");
				}
			}else if(group.getGrouptype() == 3){
				if(pGroup.getGrouptype() != 2){
					return new SysError("该位置不可创建园区型分组");
				}
			}else if(group.getGrouptype() == 4){
				if(pGroup.getGrouptype() > 1){
					return new SysError("该位置不可创建农资型分组");
				}
			}else if(group.getGrouptype() == 5){
				if(pGroup.getGrouptype() > 1){
					return new SysError("该位置不可创建检测型分组");
				}
			}
		}
		LoginUser loginUser = getLoginUser(request);
		SysError info = sysGroupService.add(group, loginUser, request);
		return info;
	}
	
	
	/**
	 * 修改分组信息
	 * @param modelMap
	 * @param groupid
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap modelMap, String groupid){
		SysGroup group = sysGroupService.get(groupid);
		modelMap.put("group", group);
		return "sys/group/group_edit";
	}
	
	
	/**
	 * 修改分组信息
	 * @param group
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid SysGroup group, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		if(!group.getPid().equals("0")){
			SysGroup pGroup = sysGroupService.get(group.getPid());
			if(group.getGrouptype() == 1){
				if(pGroup.getGrouptype() > 1){
					return new SysError("该位置不可创建地区型分组");
				}
			}else if(group.getGrouptype() == 2){
				if(pGroup.getGrouptype() > 1){
					return new SysError("该位置不可创建企业型分组");
				}
			}else if(group.getGrouptype() == 3){
				if(pGroup.getGrouptype() != 2){
					return new SysError("该位置不可创建园区型分组");
				}
			}else if(group.getGrouptype() == 4){
				if(pGroup.getGrouptype() > 1){
					return new SysError("该位置不可创建农资型分组");
				}
			}else if(group.getGrouptype() == 5){
				if(pGroup.getGrouptype() > 1){
					return new SysError("该位置不可创建检测型分组");
				}
			}
		}
		LoginUser loginUser = getLoginUser(request);
		SysError info = sysGroupService.update(group, loginUser, request);
		return info;
	}
	
	
	/**
	 * 删除分组信息
	 * @param groupid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] groupid, HttpServletRequest request){
		if(groupid==null || groupid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return sysGroupService.delete(groupid, getLoginUser(request), request);
	}
	
	
	/**
	 * 判断分组名称是否被使用
	 * 
	 * @param groupname
	 * @param groupid
	 * @param pid
	 * @return 0未被使用、1已被使用
	 */
	@RequestMapping(value="/isNameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNameDuplicate(String groupname, String groupid, String pid){
		boolean existed = sysGroupService.isNameDuplicate(groupname, groupid, pid);
		return existed ? "1":"0";
	}
	
	
	/**
	 * 地区下拉列表数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getGroupData.json", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public List<SysGroup> geGroupData(HttpServletRequest request){
		return sysGroupService.geGroupData(getLoginUser(request));
	}
	
}
